﻿using System;
using System.Collections.Specialized;
using System.Reflection;

namespace OneWorks.Extension
{
    public static class TypeExtensions
    {
        public static NameValueCollection ToDescriptionNameValue(this Type type)
        {
            if (!type.IsEnum) return new NameValueCollection();
            NameValueCollection nameValueCollection = new NameValueCollection();
            FieldInfo[] fieldInfos = type.GetFields();
            foreach (FieldInfo fieldInfo in fieldInfos)
            {
                if (fieldInfo.Name == "value__")
                    continue;
                var name = fieldInfo.Name;
                var value = Enum.Parse(type, fieldInfo.Name).ToDescriptionByEnum();
                nameValueCollection.Add(name, value);
            }
            return nameValueCollection;
        }
    }
}
